package mango.orm.connect;

import java.text.MessageFormat;

import mango.Mango;
import mango.functions.FuncString;


public final class ConnectionParam {
    public static final String CONNECTOR_DBTYPE = "connector.{0}.dbtype";
    public static final String CONNECTOR_DRIVER = "connector.{0}.driver";
    public static final String CONNECTOR_URL = "connector.{0}.url";
    public static final String CONNECTOR_USER = "connector.{0}.user";
    public static final String CONNECTOR_PASSWORD = "connector.{0}.password";
    public static final String CONNECTOR_JDNI = "connector.{0}.jndi";
    public static final String CONNECTOR_TIMEOUT = "connector.{0}.logintimeout";
    public static final String CONNECTOR_MAXCONNECTION = "connector.{0}.maxconnection";
    public static final String CONNECTOR_MINCONNECTION = "connector.{0}.minconnection";
    public static final String CONNECTOR_RELEASETIMEOUT = "connector.{0}.releasetimeout";
    public static final String CONNECTOR_TESTSQL = "connector.{0}.testsql";
    
    private String connName;
    private String dbType;
    private String driver;
    private String jndi;
    private String url;
    private String user;
    private String password;
    private int loginTimeout;
    private int releaceTimeout;
    private int maxConnection;
    private int minConnection;
    private boolean jndiconn;
    private String testSql;
    
    public ConnectionParam(String name) {
        connName = name;
        dbType = Mango.getProperty(MessageFormat.format(CONNECTOR_DBTYPE, connName));
        driver = Mango.getProperty(MessageFormat.format(CONNECTOR_DRIVER, connName));
        jndi = Mango.getProperty(MessageFormat.format(CONNECTOR_JDNI, connName));
        url = Mango.getProperty(MessageFormat.format(CONNECTOR_URL, connName));
        user = Mango.getProperty(MessageFormat.format(CONNECTOR_USER, connName));
        password = Mango.getProperty(MessageFormat.format(CONNECTOR_PASSWORD, connName));
        testSql = Mango.getProperty(MessageFormat.format(CONNECTOR_TESTSQL, connName));
        String timeout = Mango.getProperty(MessageFormat.format(CONNECTOR_TIMEOUT, connName));
        String maxConnection = Mango.getProperty(MessageFormat.format(CONNECTOR_MAXCONNECTION, connName));
        String minConnection = Mango.getProperty(MessageFormat.format(CONNECTOR_MINCONNECTION, connName));
        String releaceTimeout = Mango.getProperty(MessageFormat.format(CONNECTOR_RELEASETIMEOUT, connName));
        if (!FuncString.isBlank(jndi)) {
            jndiconn = true;
        }
        if (FuncString.isBlank(timeout)) {
            this.loginTimeout = 3;
        }
        else {
            this.loginTimeout = Integer.parseInt(timeout);
        }
        if (FuncString.isBlank(maxConnection)) {
            this.maxConnection = 10;
        }
        else {
            this.maxConnection = Integer.parseInt(maxConnection);
        }
        if (FuncString.isBlank(minConnection)) {
            this.minConnection = 3;
        }
        else {
            this.minConnection = Integer.parseInt(maxConnection);
        }
        if (FuncString.isBlank(releaceTimeout)) {
            this.releaceTimeout = 600;
        }
        else {
            this.releaceTimeout = Integer.parseInt(releaceTimeout);
        }
        
        if(FuncString.isBlank(testSql)) {
            if(Connector.DB_ORACLE.equalsIgnoreCase(dbType)) {
                testSql = "select sysdate from dual";
            }
            else if(Connector.DB_MYSQL.equalsIgnoreCase(dbType)) {
                testSql = "select CURRENT_DATE";
            }
        }
    }
    
    public String getConnName() {
        return connName;
    }

    public String getDbType() {
        return dbType;
    }

    public String getDriver() {
        return driver;
    }

    public String getJndi() {
        return jndi;
    }

    public String getUrl() {
        return url;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public int getLoginTimeout() {
        return loginTimeout;
    }

    public int getReleaceTimeout() {
        return releaceTimeout;
    }

    public int getMaxConnection() {
        return maxConnection;
    }
    
    public int getMinConnection() {
        return minConnection;
    }

    public boolean isJndiconn() {
        return jndiconn;
    }

    public String getTestSql() {
        return testSql;
    }
}
